package com.example.demo.auth;

import java.io.Serializable;
import java.util.HashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@SuppressWarnings("serial")
public class TokenResult implements Serializable {
	
	private final static Logger log = LoggerFactory.getLogger(TokenResult.class);
	
	// success
	public static final Integer CODE = 0;
	// appid无效
	public static final Integer INVALID_APPID_CODE = 100;
	// token无效
	public static final Integer INVALID_TOKEN_CODE = 200;
	
	public static final Integer INVALID_timestamp = 300;
	// sign 无效
	public static final Integer INVALID_SIGN_CODE = 400;
	// 程序异常
	public static final Integer ERR_CODE = 500;
	// sql save
	public static final Integer nonce = 600;

	private String code;
	private String msg;
	private Object data;

	private TokenResult() {

	}

	private static TokenResult instance = null;

	public static TokenResult gettokenResult() {
		if (instance == null) {
			synchronized (TokenResult.class) {
					instance = new TokenResult();
					log.info("创建tokenReuslt对象");
			}
		}
		return instance;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * 正确返回token 的json字符串
	 * {"code":"0","msg":"ok","access_token":"b302050d37d553a102dcdf12953634748b2c7dc01e1e66537399398e0ead1d2c","expires_in":"7200"}
	 * 
	 * @param access_token
	 * @return
	 */
	public String getResultMsg(String access_token, long expires_in) {
		
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		HashMap<String, Object> hashMap1 = new HashMap<String, Object>();
		hashMap.put("code", CODE);
		hashMap.put("msg", "success");
		hashMap1.put("token", access_token);
		hashMap1.put("expire", expires_in);
		hashMap.put("data",hashMap1);
		return JsonKit.general.toJson(hashMap);
	}

	/**
	 * 程序错误 {"code":"001","msg":"token"}
	 * 
	 * @return
	 */
	public String getErrMsg() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code", ERR_CODE);
		hashMap.put("msg", "*****(服务器内部处理出错，建议重试)");
		return JsonKit.general.toJson(hashMap);
	}

	/**
	 * appid 无效 {"code":"003","msg":"invalid appid"}
	 * 
	 * @return
	 */
	public String getInvalidAppidMsg() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code", INVALID_APPID_CODE);
		hashMap.put("msg", "invalid appid(appid非法，请检查)");
		return JsonKit.general.toJson(hashMap);
	}
	
	public String getInvalidnonceMsg() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code", nonce);
		hashMap.put("msg", "invalid nonce");
		return JsonKit.general.toJson(hashMap);
	}
	
	/**
	 * 验证timestamp ||| timestamp 无效 {"code":"003","msg":"invalid appid"}
	 * @param da
	 * @param timestamp
	 * @return
	 * @throws Exception
	 */
	public String getInvalidtimestampMsg() throws Exception{
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code",INVALID_timestamp);
		hashMap.put("msg","invalid timestamp");
		return JsonKit.general.toJson(hashMap);
	}

	/**
	 * token过期 {"code":"004","msg":"token is Expired","expires_in":0}
	 * 
	 * @return
	 */
	public String getIsExpiredMsg() {
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code", INVALID_TOKEN_CODE);
		hashMap.put("msg", "invalid token(token is Expired)");
		return JsonKit.general.toJson(hashMap);
	}

	public static void main(String[] args) {
		String s = TokenResult.gettokenResult().getIsExpiredMsg();
		log.info(TokenResult.gettokenResult().getInvalidSignMsg());
		log.info(TokenResult.gettokenResult().getResultMsg("", 123123));
		
		log.info(TokenResult.gettokenResult().getInvalidnonceMsg());
	}

	/**
	 * 
	 * {"code":"005","msg":"invalid sign","expires_in":0} 签名无效
	 * 
	 * @return
	 */
	public String getInvalidSignMsg() {
		
		HashMap<String, Object> hashMap = new HashMap<String, Object>();
		hashMap.put("code", INVALID_SIGN_CODE);
		hashMap.put("msg", "invalid sign");
		return JsonKit.general.toJson(hashMap);
	}

}
